package com.jprocms.module.cms.controller.directive;

import com.jpro.framework.common.pojo.PageResult;
import com.jpro.framework.common.util.collection.CollectionUtils;
import com.jprocms.module.cms.controller.admin.content.tagvo.TagExportReqVO;
import com.jprocms.module.cms.controller.admin.content.tagvo.TagPageReqVO;
import com.jprocms.module.cms.dal.dataobject.content.TagDO;
import com.jprocms.module.cms.service.content.TagService;
import com.jprocms.module.cms.util.Freemarkers;
import com.jprocms.module.cms.util.FrontContextUtil;
import com.jprocms.module.cms.web.Directives;
import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * TAG列表 自定义标签
 *
 * @author jprocms
 */
public class TagListDirective implements TemplateDirectiveModel {
    /**
     * 站点ID
     */
    public static final String SITE_ID = "siteId";
    /**
     * 名称。字符串(String)。
     */
    public static final String NAME = "name";

    protected void doExecute(Environment env, Map<String, TemplateModel> params, TemplateModel[] loopVars,
                             TemplateDirectiveBody body, boolean isPage) throws TemplateException, IOException {
        Freemarkers.requireLoopVars(loopVars);
        Freemarkers.requireBody(body);

        Long defSiteId = FrontContextUtil.getSiteId(env);
        Long siteId = Directives.getLong(params, SITE_ID, defSiteId);
        String tagName = Directives.getString(params, NAME);
        if (isPage) {
            int page = Directives.getPage(params, env);
            int pageSize = Directives.getPageSize(params, env);
            TagPageReqVO pageReqVO = new TagPageReqVO();
            pageReqVO.setSiteId(siteId);
            pageReqVO.setTagName(tagName);
            pageReqVO.setPageNo(page);
            pageReqVO.setPageSize(pageSize);
            PageResult<TagDO> tagPage = service.getTagPage(pageReqVO);
            Directives.setTotalPages(tagPage.getPages());
            tagPage.setList(CollectionUtils.filterList(tagPage.getList(), tagDO -> StringUtils.hasText(tagDO.getTagName())));
            loopVars[0] = env.getObjectWrapper().wrap(tagPage);
        } else {
            TagExportReqVO tagExportReqVO = new TagExportReqVO(tagName, siteId);
            List<TagDO> list = service.getTagList(tagExportReqVO);
            List<TagDO> tagDOS = CollectionUtils.filterList(list, (tagDO) -> StringUtils.hasText(tagDO.getTagName()));
            loopVars[0] = env.getObjectWrapper().wrap(tagDOS);
        }
        body.render(env.getOut());
    }

    @SuppressWarnings("unchecked")
    @Override
    public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body)
            throws TemplateException, IOException {
        doExecute(env, params, loopVars, body, false);
    }


    private final TagService service;

    public TagListDirective(TagService service) {
        this.service = service;
    }
}
